import Clipboard from 'clipboard'

function handleClipboard(text, event, onSuccess, onError) {
	event = event || {}
	const clipboard = new Clipboard(event.target, {
		text: () => text
	})
	clipboard.on('success', () => {
		onSuccess()
		clipboard.off('error')
		clipboard.off('success')
		clipboard.destroy()
	})
	clipboard.on('error', () => {
		onError()
		clipboard.off('error')
		clipboard.off('success')
		clipboard.destroy()
	})
	clipboard.onClick(event)
}

export {
	handleClipboard
}
